'use strict';

const Service = require('egg').Service;

class QuestionCategoryService extends Service {
  // 获取所有分类
  async findAll() {
    const { ctx } = this;
    return await ctx.model.QuestionCategory.findAll({
      order: [['created_at', 'DESC']],
    });
  }

  // 根据ID获取分类
  async findById(id) {
    const { ctx } = this;
    return await ctx.model.QuestionCategory.findByPk(id);
  }

  // 创建分类
  async create(data) {
    const { ctx } = this;
    return await ctx.model.QuestionCategory.create(data);
  }

  // 更新分类
  async update(id, data) {
    const { ctx } = this;
    const category = await ctx.model.QuestionCategory.findByPk(id);
    if (category) {
      await category.update(data);
      return category;
    }
    return null;
  }

  // 删除分类
  async delete(id) {
    const { ctx } = this;
    const category = await ctx.model.QuestionCategory.findByPk(id);
    if (category) {
      await category.destroy();
      return true;
    }
    return false;
  }
}

module.exports = QuestionCategoryService;
